const { Sequelize } = require('sequelize')
const { dbConfig } = require('../config/index')
let config 
if(process.env.NODE_ENV=="production"){
  config= {
    port:3306,
    host: 'app-mysql',
    dialect: 'mysql'
  }
}
else{
  config={
    host: 'localhost',
    dialect: 'mysql'
  }
}
// 生产环境使用连接池
if (process.env.NODE_ENV == 'production') {
  config.pool = {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  }
}
const sequelize = new Sequelize(dbConfig.databaseName, dbConfig.username, dbConfig.password, config)
if(process.env.NODE_ENV!="test"){
  sequelize.authenticate().then((res) => {
    console.log('mysql connect success')
    return sequelize.sync({ force: true })
  })
  .then((res) => {
    console.log('model sync success')
  })

}

module.exports = sequelize
